neutron-control-pkg:
  pkg.installed:
    - names:
      - openstack-neutron
      - openstack-neutron-ml2
      - openstack-neutron-linuxbridge
      - ebtables

neutron-control-neutron-conf:
  file.managed:
    - name: /etc/neutron/neutron.conf
    - source: salt://openstack/queen/neutron-control/files/neutron.conf
    - user: root
    - group: neutron
    - mode: 640
    - template: jinja
    - defaults:
      MYSQL_IP: {{ pillar['mysql']['MYSQL_IP'] }}
      NEUTRON_DB_PASS: {{ pillar['neutron']['NEUTRON_DB_PASS'] }}
      NEUTRON_DB_USER: {{ pillar['neutron']['NEUTRON_DB_USER'] }}
      NEUTRON_DB_NAME: {{ pillar['neutron']['NEUTRON_DB_NAME'] }}
      RABBITMQ_OPENSTACK_USER: {{ pillar['rabbitmq']['RABBITMQ_OPENSTACK_USER'] }}
      RABBITMQ_OPENSTACK_PASS: {{ pillar['rabbitmq']['RABBITMQ_OPENSTACK_PASS'] }}
      RABBITMQ_IP: {{ pillar['rabbitmq']['RABBITMQ_IP'] }}
      MEMCACHED_IP: {{ pillar['memcached']['MEMCACHED_IP'] }}
      KEYSTONE_IP: {{ pillar['keystone']['KEYSTONE_IP'] }}
      NEUTRON_KEYSTONE_USER: {{ pillar['neutron']['NEUTRON_KEYSTONE_USER'] }}
      NEUTRON_KEYSTONE_PASS: {{ pillar['neutron']['NEUTRON_KEYSTONE_PASS'] }}
      NOVA_KEYSTONE_USER: {{ pillar['nova']['NOVA_KEYSTONE_USER'] }}
      NOVA_KEYSTONE_PASS: {{ pillar['nova']['NOVA_KEYSTONE_PASS'] }}
    - require:
      - pkg: neutron-control-pkg

neutron-control-ml2-conf:
  file.managed:
    - name: /etc/neutron/plugins/ml2/ml2_conf.ini
    - source: salt://openstack/queen/neutron-control/files/ml2_conf.ini
    - user: root
    - group: neutron
    - mode: 640
    - require:
      - pkg: neutron-control-pkg

neutron-control-ml2-symlink:
  file.symlink:
    - name: /etc/neutron/plugin.ini
    - target: /etc/neutron/plugins/ml2/ml2_conf.ini
    - require:
      - file: neutron-control-ml2-conf


neutron-control-linuxbridge-conf:
  file.managed:
    - name: /etc/neutron/plugins/ml2/linuxbridge_agent.ini
    - source: salt://openstack/queen/neutron-control/files/linuxbridge_agent.ini
    - user: root
    - group: neutron
    - mode: 640
    - require:
      - pkg: neutron-control-pkg

neutron-control-fix-tornado:
  file.managed:
    - name: /usr/lib/python2.7/site-packages/tenacity/__init__.py
    - source: salt://openstack/queen/neutron-control/files/__init__.py
    - user: root
    - group: root
    - mode: 644
    - require:
      - pkg: neutron-control-pkg

neutron-control-dhcp-conf:
  file.managed:
    - name: /etc/neutron/dhcp_agent.ini
    - source: salt://openstack/queen/neutron-control/files/dhcp_agent.ini
    - user: root
    - group: neutron
    - mode: 640
    - require:
      - pkg: neutron-control-pkg

neutron-control-metadata-conf:
  file.managed:
    - name: /etc/neutron/metadata_agent.ini
    - source: salt://openstack/queen/neutron-control/files/metadata_agent.ini
    - user: root
    - group: neutron
    - mode: 640
    - template: jinja
    - defaults:
      NOVA_IP: {{ pillar['nova']['NOVA_IP'] }}
      NEUTRON_METADATA_SECRET: {{ pillar['neutron']['NEUTRON_METADATA_SECRET'] }}
    - require:
      - pkg: neutron-control-pkg


neutron-db-sync:
  file.managed:
    - name: /etc/script/neutron-db-sync.sh
    - source: salt://openstack/queen/neutron-control/files/neutron-db-sync.sh
    - user: root
    - group: root
    - mode: 700
  cmd.run:
    - name: bash /etc/script/neutron-db-sync.sh && touch /etc/lock/neutron-db-sync.lock
    - require:
      - file: neutron-control-neutron-conf
      - file: neutron-control-ml2-symlink
      - file: neutron-control-linuxbridge-conf
      - file: neutron-control-dhcp-conf
      - file: neutron-control-metadata-conf
    - unless : test -f /etc/lock/neutron-db-sync.lock

neutron-server-service:
  service.running:
    - name: neutron-server
    - enable: True
    - require:
      - cmd: neutron-db-sync
    - watch:
      - file: neutron-control-neutron-conf
      - file: neutron-control-ml2-conf
      - file: neutron-control-linuxbridge-conf

neutron-linuxbridge-agent-service:
  service.running:
    - name: neutron-linuxbridge-agent
    - enable: True
    - require:
      - cmd: neutron-db-sync
      - file: neutron-control-fix-tornado
    - watch:
      - file: neutron-control-linuxbridge-conf

neutron-dhcp-agent-service:
  service.running:
    - name: neutron-dhcp-agent
    - enable: True
    - require:
      - cmd: neutron-db-sync
    - watch:
      - file: neutron-control-dhcp-conf

neutron-metadata-agent-service:
  service.running:
    - name: neutron-metadata-agent
    - enable: True
    - require:
      - cmd: neutron-db-sync
    - watch:
      - file: neutron-control-metadata-conf


neutron-service-endpoint:
  file.managed:
    - name: /etc/script/neutron-service-endpoint.sh
    - source: salt://openstack/queen/neutron-control/files/neutron-service-endpoint.sh
    - user: root
    - group: root
    - mode: 700
    - template: jinja
    - defaults:
      NEUTRON_IP: {{ pillar['neutron']['NEUTRON_IP'] }}
  cmd.run:
    - name: bash /etc/script/neutron-service-endpoint.sh && touch /etc/lock/neutron-service-endpoint.lock
    - require:
      - service: neutron-server-service
      - service: neutron-linuxbridge-agent-service
      - service: neutron-dhcp-agent-service
      - service: neutron-metadata-agent-service
    - unless : test -f /etc/lock/neutron-service-endpoint.lock
